package com.swpu.vd.service.Impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.swpu.servicebase.ExceptionHandler.GuliException;
import com.swpu.vd.Utils.ConstantVodUtils;
import com.swpu.vd.Utils.InitObj;
import com.swpu.vd.service.VodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: ygq
 * @Date: 2021/10/18 20:05
 */
@Service
@Slf4j
public class VodServiceImpl implements VodService {
    @Override
    public String uploadfile(MultipartFile file) {
//        String title = " ";
//        InputStream inputStream = file.getInputStream();
//        String fileName =  file.getOriginalFilename();
//        title  = fileName.substring(0, fileName.lastIndexOf("."));
//        UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
//        UploadVideoImpl uploader = new UploadVideoImpl();
//        UploadStreamResponse response = uploader.uploadStream(request);
////        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
//        String VedioId = response.getVideoId();
//        return VedioId;
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET,
                    title, originalFilename, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                log.warn(errorMessage);
                if(StringUtils.isEmpty(videoId)){
                    throw new GuliException(20001, errorMessage);
                }
            }

            return videoId;
        } catch (IOException e) {
            throw new GuliException(20001, "guli vod 服务上传失败");
        }

    }

    @Override
    public void remoVedioById(String id) {
        try {
            //初始化对象
            DefaultAcsClient client = InitObj.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (Exception e){
            throw new GuliException(20001,"删除视频失败");
        }
    }

    //批量删除视频
    //传入删除的视频的id值 用逗号隔开
    @Override
    public void deletebatch(List<String> videoList) {
        try {
            //初始化对象
            DefaultAcsClient client = InitObj.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            String join = org.apache.commons.lang.StringUtils.join(videoList.toArray(), ",");

            request.setVideoIds(join);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (Exception e){
            throw new GuliException(20001,"删除视频失败");
        }
    }
}
